package com.carpedil.log;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/** @author Administrator */
@Component
@Aspect
public class LogEnhancer {
  @Pointcut("execution(* com.carpedil.dao..*(..))")
  public void pointcuts() {
    // ...
  }

  @Before("pointcuts()")
  public void logBefore() {
    System.out.println("前置通知......");
  }

  @AfterReturning("pointcuts()")
  public void logAfterReturning() {
    System.out.println("后置通知......");
  }

  @After("pointcuts()")
  public void logAfter() {
    System.out.println("最后通知......");
  }

  @Around("pointcuts()")
  public void aroundLog(ProceedingJoinPoint joinPoint) {
    System.out.println("环绕通知开始----");
    try {
      joinPoint.proceed();
      System.out.println(joinPoint.getTarget());
    } catch (Throwable throwable) {
      System.out.println("有异常");
    }
    System.out.println("环绕通知结束----");
  }

  @AfterThrowing(value = "pointcuts()", throwing = "e")
  public void logException(Throwable e) {
    System.out.println("异常通知: " + e.getMessage());
  }
}
